package com.gis.util;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class MessageSender {
	
	/**
	 * send e-mail
	 * @param mailContext
	 */
	public static void send(MailContext mailContext) {
		Properties props = new Properties();                    
        props.setProperty("mail.transport.protocol", mailContext.getProtocol());
        props.setProperty("mail.smtp.host", mailContext.getHost());
        props.setProperty("mail.smtp.auth", "true");
        Session session = Session.getInstance(props);
		
		MimeMessage message = new MimeMessage(session);
        try {
			message.setFrom(new InternetAddress(mailContext.getFrom(), mailContext.getUsername(), "UTF-8"));
			message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(mailContext.getTo(), mailContext.getTo(), "UTF-8"));
	        message.setSubject(mailContext.getSubject(), "UTF-8");
	        message.setContent(mailContext.getContent(), "text/html;charset=UTF-8");
	        message.setSentDate(new Date());
	        message.saveChanges();
	        
	        Transport transport = session.getTransport();
	        transport.connect(mailContext.getFrom(), mailContext.getPassword());
	        transport.sendMessage(message, message.getAllRecipients());
	        transport.close();
		} catch (UnsupportedEncodingException | MessagingException e) {
			e.printStackTrace();
		}
        
	}
}